
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-29 下午3:01:01 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.data.ArticleSummaryAnalysisData;
import org.alipay4j.model.data.MenuAnalysisData;
import org.alipay4j.model.data.SingleArticleAnalysisData;
import org.alipay4j.model.data.UserAnalysisData;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 数据分析组件 
 * @Description: 数据分析组件
 * @author heliang
 * @date 2019-8-29 下午3:01:01 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */

public class DataComponent extends AbstractComponent {

    public DataComponent(Alipay alipay) {
        super(alipay);
    }
    
    /**
    * @Title: 图文分析-按文章查询数据接口 
    * @Description: 开发者可以通过本接口查询最近30天内发布的图文消息的数据，数据包括：发布时间、送达人数、阅读人数、分享人数等。
    * @author heliang
    * @param begin_date 开始日期
    * @param end_date 结束日期，开始日期/结束日期时间跨度最大30天
    * @return SingleArticleAnalysisData
    * @throws AlipayException 
     */
    public List<SingleArticleAnalysisData> articleData(String begin_date,String end_date) throws AlipayException {
        if (ObjectHelper.isEmpty(begin_date)) {
            throw new IllegalArgumentException("begin_date can't be null or empty");
        }
        if (ObjectHelper.isEmpty(end_date)) {
            throw new IllegalArgumentException("end_date can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("begin_date", begin_date);
        body.put("end_date", end_date);
        body.put("method", "alipay.open.public.singlearticle.data.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_singlearticle_data_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_singlearticle_data_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<SingleArticleAnalysisData> datas = new ArrayList<SingleArticleAnalysisData>();
        JSONArray array = bodyRes.getJSONArray("data_list");
        for(int i=0;i<array.size();i++) {
            SingleArticleAnalysisData obj = JSONObject.toJavaObject(array.getJSONArray(i), SingleArticleAnalysisData.class);
            datas.add(obj);
        }
        return datas;
    }
    
    /**
    * @Title: 用户分析数据查询接口 
    * @Description: 通过此接口，开发者可以根据时间查询30天内的关注用户人数、取消关注用户人数、净增关注人数、累计关注人数信息
    * @author heliang 
    * @param begin_date 开始日期
    * @param end_date 结束日期，开始日期/结束日期时间跨度最大30天
    * @return UserAnalysisData
    * @throws AlipayException 
     */
    public List<UserAnalysisData> userData(String begin_date,String end_date) throws AlipayException {
        if (ObjectHelper.isEmpty(begin_date)) {
            throw new IllegalArgumentException("begin_date can't be null or empty");
        }
        if (ObjectHelper.isEmpty(end_date)) {
            throw new IllegalArgumentException("end_date can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("begin_date", begin_date);
        body.put("end_date", end_date);
        body.put("method", "alipay.open.public.user.data.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_singlearticle_data_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_singlearticle_data_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<UserAnalysisData> datas = new ArrayList<UserAnalysisData>();
        JSONArray array = bodyRes.getJSONArray("data_list");
        for(int i=0;i<array.size();i++) {
            UserAnalysisData obj = JSONObject.toJavaObject(array.getJSONArray(i), UserAnalysisData.class);
            datas.add(obj);
        }
        return datas;
    }

    /**
    * @Title: 图文分析-按时间查询数据接口 
    * @Description: 开发者可以通过图文分析接口查询30天内，所有历史发布的图文消息的数据，包括送达人数、曝光人数、图文阅读人数、分享人数等信息。
    * @author heliang
    * @param begin_date 开始日期
    * @param end_date 结束日期，开始日期/结束日期时间跨度最大30天
    * @return  ArticleSummaryAnalysisData
    * @throws AlipayException 
     */
    public List<ArticleSummaryAnalysisData> summaryData(String begin_date,String end_date) throws AlipayException {
        if (ObjectHelper.isEmpty(begin_date)) {
            throw new IllegalArgumentException("begin_date can't be null or empty");
        }
        if (ObjectHelper.isEmpty(end_date)) {
            throw new IllegalArgumentException("end_date can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("begin_date", begin_date);
        body.put("end_date", end_date);
        body.put("method", "alipay.open.public.articlesummary.data.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_articlesummary_data_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_articlesummary_data_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<ArticleSummaryAnalysisData> datas = new ArrayList<ArticleSummaryAnalysisData>();
        JSONArray array = bodyRes.getJSONArray("data_list");
        for(int i=0;i<array.size();i++) {
            ArticleSummaryAnalysisData obj = JSONObject.toJavaObject(array.getJSONArray(i), ArticleSummaryAnalysisData.class);
            datas.add(obj);
        }
        return datas;
    }
    
    
    /**
    * @Title: 菜单分析数据查询接口 
    * @Description: 开发者可以根据本接口查询30天内菜单的相关数据，包括菜单点击人数和次数
    * @author heliang
    * @param begin_date 开始日期
    * @param end_date 结束日期，开始日期/结束日期时间跨度最大30天
    * @return MenuAnalysisData
    * @throws AlipayException 
     */
    public List<MenuAnalysisData> menuData(String begin_date,String end_date) throws AlipayException {
        if (ObjectHelper.isEmpty(begin_date)) {
            throw new IllegalArgumentException("begin_date can't be null or empty");
        }
        if (ObjectHelper.isEmpty(end_date)) {
            throw new IllegalArgumentException("end_date can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("begin_date", begin_date);
        body.put("end_date", end_date);
        body.put("method", "alipay.open.public.menu.data.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_menu_data_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_menu_data_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<MenuAnalysisData> datas = new ArrayList<MenuAnalysisData>();
        JSONArray array = bodyRes.getJSONArray("data_list");
        for(int i=0;i<array.size();i++) {
            MenuAnalysisData obj = JSONObject.toJavaObject(array.getJSONArray(i), MenuAnalysisData.class);
            datas.add(obj);
        }
        return datas;
    }
}
